package subject_set.weekly_competition.day10_2;

/**
 * @author haomin
 * @date 2022/10/02 10:28
 **/
public class Num3 {
    class Solution {
        public int minimizeXor(int num1, int num2) {
            int min = 0;
            int minXOR = Integer.MAX_VALUE;
            int find2 = findOne(num2);
            for(int i = 0; i <= Integer.MAX_VALUE; ++i){
                if(findOne(i) == find2 && minXOR > (i ^ num1)){
                    minXOR = i ^ num1;
                    min = i;
                }
            }
            return min;
        }
        private int findOne (int num){
            int res = 0;
            int i = 32;
            while(i-- > 0){
                if((num & 1) == 1) res++;
                num >>= 1;
            }
            return res;
        }
    }
    class Solution2 {
        public int minimizeXor(int num1, int num2) {
            int count1 = Integer.bitCount(num2);
            int i = 0;
            while(true){
                int x = i ^ num1;
                int count2 = Integer.bitCount(x);
                if(count1 == count2){
                    return x;
                }
                i++;
            }
        }
    }
}